package com.itstation.neordo.ims.dao;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.itstation.neordo.ims.model.Actor;

@Repository
public class ActorDAOImpl implements ActorDAO {
	
	@Autowired
	private SessionFactory sessionFactory;
	
	private Session getCurrentSession(){
		return sessionFactory.getCurrentSession();
	}

	@Override
	public void addActor(Actor actor) {
		getCurrentSession().save(actor);
	}

	@Override
	public void updateActor(Actor actor) {
		Actor actorToUpdate = getActor(actor.getActorId());
		actorToUpdate.setFirstName(actor.getFirstName());
		actorToUpdate.setLastName(actor.getLastName());
		actorToUpdate.setLastUpdate(actor.getLastUpdate());
		
		getCurrentSession().update(actorToUpdate);
	}

	@Override
	public Actor getActor(int id) {
		Actor actor = (Actor) getCurrentSession().get(Actor.class, id);
		
		return actor;
	}

	@Override
	public void deleteActor(int id) {
		Actor actor = getActor(id);
		if(actor!=null) getCurrentSession().delete(actor);
	}

	@SuppressWarnings("unchecked")
	public List<Actor> getActors() {
		System.out.println("getActors called");
		return getCurrentSession().createQuery("from Actor").list();
	}

}
